#include "server.h"
int mkDir(char *realPath,MYSQL* mysql,char* username,ENTRY *entry){
    //realPath=usr/dir1/dir2, entry->data = dir3;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char oldpath[1024]={0};
    char newpath[4096]={0};
    char sql[4096]={0};
    char testPath[4096]={0};
    sprintf(testPath,"%s/%s",realPath,entry->data);
    sprintf(sql,"select path from file where path='%s';",testPath);
    int ret =mysql_query(mysql,sql);
    MYSQL_RES* res1;
    res1=mysql_store_result(mysql);
    int numrow=mysql_num_rows(res1);
    if(numrow!=0){
        memset(sql,0,sizeof(sql));
        sprintf(sql,"update file set tomb = 0 where path ='%s';",testPath);
        ret=mysql_query(mysql,sql);
        if(ret!=0)
        {
            fprintf(stderr,"mysql connect error1:%s\n",mysql_error(mysql));
        }
        return 0;
    }

    sprintf(sql,"select path,id from file where path='%s';",realPath);
    ret=mysql_query(mysql,sql);
    if(ret!=0)
    {
        fprintf(stderr,"mysql connect error2:%s\n",mysql_error(mysql));
    }

    res=mysql_store_result(mysql);
    row=mysql_fetch_row(res);

    int preid=atoi(row[1]);
    sprintf(oldpath,"%s",row[0]);
    printf("oldpath=%s\n",oldpath);
    sprintf(newpath,"%s/%s",realPath,entry->data);
    printf("newpath=%s\n",newpath);
    //char* username="ljx";
    memset(sql,0,sizeof(sql));
    sprintf(sql,"insert into file (filename,type,pre_id,user,path,tomb,md5)values('%s','d','%d','%s','%s',0,'0');",entry->data,preid,username,newpath);
    ret=mysql_query(mysql,sql);
    if(ret!=0)
    {
        fprintf(stderr,"mysql connect error:%s\n",mysql_error(mysql));
    }
    return 0;
}

int rmDir(char *realPath,MYSQL* mysql,ENTRY *entry){
    //realPath=usr/dir1/dir2, entry->data = dir3;
    
    char sql[4096]={0};
    char path[4096]={0};
    sprintf(path,"%s/%s",realPath,entry->data);
    sprintf(sql,"select filename from file where path='%s';",path);
    int ret=mysql_query(mysql,sql);
    if(ret!=0)
    {
        fprintf(stderr,"mysql connect error1:%s\n",mysql_error(mysql));
    }
    MYSQL_RES *res;
    res=mysql_store_result(mysql);
    memset(sql,0,sizeof(sql));
    sprintf(sql,"update file set tomb = 1 where path ='%s';",path);
    ret=mysql_query(mysql,sql);
    if(ret!=0)
    {
        fprintf(stderr,"mysql connect error:%s\n",mysql_error(mysql));
    }
    
    return 0;
}

int reMove(char *realPath,MYSQL* mysql,ENTRY *entry){
    //realPath=usr/dir1/dir2, entry->data = file1;
   rmDir(realPath,mysql,entry);
    return 0;
}

int mPwd(int netfd,char *realPath,MYSQL* mysql){
    //realPath=usr/dir1/dir2
    
    MYSQL_RES *res;
    MYSQL_ROW row;
    char sql[4096]={0};
    sprintf(sql,"select path from file where path='%s';",realPath);
    int ret = mysql_query(mysql,sql);
    if(ret!=0){
        fprintf(stderr,"mysql connect error:%s\n",mysql_error(mysql));
    }

    res = mysql_store_result(mysql);
    char buff[1024]={0};
    row =mysql_fetch_row(res);
    sprintf(buff,"%s",row[0]);
    printf("buff=%s\n",buff);
    
    train_t train;
    bzero(&train,sizeof(train));
    sprintf(train.data,"当前路径是%s",buff);
    train.dataLength=strlen(train.data);
    send(netfd,&train,sizeof(int)+train.dataLength,MSG_NOSIGNAL);
    return 0;
}
